import java.io.*;
import java.net.Socket;
public class ClientSend {

    public static void main(String[] args) {
        Socket s = null;
        try {
            s = new Socket("192.168.56.1", 12345);
        }catch (IOException e) {
            System.out.println("未连接到服务器");
        }
        try {
            DataInputStream input = new DataInputStream(s.getInputStream());
            System.out.print("请输入: \t");
            String str = new BufferedReader(new InputStreamReader(System.in)).readLine();
            MyBC turner = new MyBC();
            String str1 = turner.turn(str);
            int length = 0, i = 0;
            while (str1.charAt(i) != '\0') {
                length++;
                i++;
            }
            String str2 = str1.substring(1, length - 1);
            SEnc senc = new SEnc(str2);//指定后缀表达式为明文字符串
            senc.encrypt();//加密
        }catch(Exception e) {
            System.out.println("客户端异常:" + e.getMessage());
        }

        File sendfile = new File("SEnc.dat");
        File sendfile1 = new File("Keykb1.dat");
        FileInputStream fis = null;
        FileInputStream fis1 = null;
        byte[] buffer = new byte[4096 * 5];
        byte[] buffer1 = new byte[4096 * 5];
        OutputStream os;

        if(!sendfile.exists() || !sendfile1.exists()){
            System.out.println("客户端：要发送的文件不存在");
            return;
        }

        try {
            fis = new FileInputStream(sendfile);
            fis1 = new FileInputStream(sendfile1);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }

        try {
            PrintStream ps = new PrintStream(s.getOutputStream());
            ps.println("111/#" + sendfile.getName() + "/#" + fis.available());
            ps.flush();
        } catch (IOException e) {
            System.out.println("服务器连接中断");
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }
        try {
            os = s.getOutputStream();
            int size = 0;
            while((size = fis.read(buffer)) != -1){
                System.out.println("客户端发送数据包，大小为" + size);
                os.write(buffer, 0, size);
                os.flush();
            }
        } catch (FileNotFoundException e) {
            System.out.println("客户端读取文件出错");
        } catch (IOException e) {
            System.out.println("客户端输出文件出错");
        }finally{
            try {
                if(fis != null)
                    fis.close();
            } catch (IOException e) {
                System.out.println("客户端文件关闭出错");
            }
        }
        try {
            PrintStream ps1 = new PrintStream(s.getOutputStream());
            ps1.println("111/#" + sendfile1.getName() + "/#" + fis1.available());
            ps1.flush();
        } catch (IOException e) {
            System.out.println("服务器连接中断");
        }

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        try {
            os = s.getOutputStream();
            int size = 0;

            while((size = fis1.read(buffer1)) != -1){
                System.out.println("客户端发送数据包，大小为" + size);
                os.write(buffer1, 0, size);
                os.flush();
            }
        } catch (FileNotFoundException e) {
            System.out.println("客户端读取文件出错");
        } catch (IOException e) {
            System.out.println("客户端输出文件出错");
        }finally{
            try {
                if(fis1 != null)
                    fis1.close();
            } catch (IOException e) {
                System.out.println("客户端文件关闭出错");
            }
        }

        try{
            DataInputStream input = new DataInputStream(s.getInputStream());
            String ret = input.readUTF();
            System.out.println("服务器端返回过来的是: " + ret);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (s == null) {
                try {
                    s.close();
                } catch (IOException e) {
                    System.out.println("客户端 finally 异常:" + e.getMessage());
                }
            }
        }
    }
}

